﻿using System;
using System.Configuration;
using System.Text.RegularExpressions;

namespace LiveNet.Configuration
{
    public class AuthorizationConfigElementValidator<T> : ConfigurationValidatorBase
        where T : struct
    {
        public override void Validate(object value)
        {
            AuthorizationConfigElement<T> val = (AuthorizationConfigElement<T>)value;

            Regex urlRegex = new Regex(@"~|((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)");

            if (!urlRegex.IsMatch(val.PermissionReturnUrl))
            {
                throw new ConfigurationErrorsException("PermissionReturnUrl is not a valid url.");
            }

            if (!urlRegex.IsMatch(val.PrivacyUrl))
            {
                throw new ConfigurationErrorsException("PrivacyUrl is not a valid url.");
            }
        }

        public override bool CanValidate(Type type)
        {
            return (typeof (AuthorizationConfigElement<T>) == type);
        }
    }
}
